<?php

namespace backend\modules\proxy_pay\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\proxy_pay\models\ProxyPay;

/**
 * ProxyPaySearch represents the model behind the search form about `backend\modules\proxy_pay\models\ProxyPay`.
 */
class ProxyPaySearch extends ProxyPay
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'status', 'amount'], 'integer'],
            [['company_code', 'created_at', 'unique_id', 'orderno', 'branchnm', 'accntno', 'accntnm', 'other_data', 'post_data', 'resp_data'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = ProxyPay::find()->orderBy(['id'=>SORT_DESC]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
//            'created_at' => $this->created_at,
            'status' => $this->status,
            'amount' => $this->amount,
        ]);

        $query->andFilterWhere(['like', 'company_code', $this->company_code])
            ->andFilterWhere(['like', 'unique_id', $this->unique_id])
            ->andFilterWhere(['like', 'orderno', $this->orderno])
            ->andFilterWhere(['like', 'branchnm', $this->branchnm])
            ->andFilterWhere(['like', 'accntno', $this->accntno])
            ->andFilterWhere(['like', 'accntnm', $this->accntnm])
            ->andFilterWhere(['like', 'other_data', $this->other_data])
            ->andFilterWhere(['like', 'post_data', $this->post_data])
            ->andFilterWhere(['like', 'resp_data', $this->resp_data]);

        if ( ! is_null($this->created_at) && strpos($this->created_at, ' - ') !== false ) {
            list($start_date, $end_date) = explode(' - ', $this->created_at);
            $query->andFilterWhere(['between', 'created_at', strtotime($start_date), strtotime($end_date.' 23:59:59')]);
        }

        return $dataProvider;
    }
}
